
package kaizone.haining.chaobohui.fragment;

import java.util.ArrayList;
import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.activity.CBHActivity;
import kaizone.haining.chaobohui.activity.DetailActivity;
import kaizone.haining.chaobohui.json.Item.PicModel;
import kaizone.haining.chaobohui.json.TushuoCategoryDO;
import kaizone.haining.chaobohui.json.TushuoItem;
import kaizone.haining.chaobohui.net.DataControl;
import kaizone.haining.chaobohui.util.ImageFetcher;
import kaizone.haining.chaobohui.widget.CBHItemTushuo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;

public class CBHTuShuoFragment extends Fragment implements DataControl.IDataUpdateListener,
        OnItemClickListener {

    public static final String DATA_SOURCE_URL = "http://api.dp-app.com/chao/remote/rest/query_tushuo_cates.htm";

    private FragmentManager mFragmentManager;

    private ArrayList<TushuoItem> mData;

    private TushuoAdapter mAdapter;

    private DataControl mDataControl;

    private ImageFetcher mImageFetcher;

    private ListView mListview;

    public CBHTuShuoFragment() {
        init();
    }

    public CBHTuShuoFragment(FragmentManager mgr) {
        mFragmentManager = mgr;
        init();
    }

    private void init() {
        mData = new ArrayList<TushuoItem>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = (View) inflater.inflate(R.layout.cbh_fragment_tushuo, null);
        mListview = (ListView) view.findViewById(R.id.listview);
        mAdapter = new TushuoAdapter();
        mListview.setAdapter(mAdapter);
        // listview.setDivider(null);
        mListview.setCacheColorHint(0);
        mListview.setOnItemClickListener(this);
        mListview.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        if (mAdapter == null)
                            return;
                        final int columnWidth = mListview.getWidth();
                        final int columnHeight = columnWidth * 1 / 2;
                        mAdapter.setItemHeight(columnHeight);

                    }
                });

        mDataControl = new DataControl(getActivity());
        mDataControl.addChangeListener(this);
        if (getActivity() instanceof CBHActivity) {
            mImageFetcher = ((CBHActivity) getActivity()).getImageFetcher();

        }
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        mDataControl.exec(DATA_SOURCE_URL);
    }

    @Override
    public Object backParse(String datastring, String requeststring) {
        if (TextUtils.isEmpty(datastring))
            return null;
        if (requeststring.equals(DATA_SOURCE_URL)) {
            TushuoCategoryDO tushuoCategoryDO = TushuoCategoryDO.parseJson(datastring);
            return tushuoCategoryDO;
        }
        return null;
    }

    @Override
    public void foreUpdate(Object result, String requeststring) {
        if (result == null)
            return;
        if (result instanceof TushuoCategoryDO) {
            TushuoCategoryDO tushuoCategoryDO = (TushuoCategoryDO) result;
            List<TushuoItem> tushuoitems = tushuoCategoryDO.getData();
            mData.removeAll(mData);
            mData.addAll(tushuoitems);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void exception(Exception e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TushuoItem item = (TushuoItem) mData.get(position);
        if (item == null)
            return;
        // String name = item.getName();
        // DetailActivity.openTushuo(getActivity(), name);
        int tushuoid = item.getId();
        DetailActivity.openTushuo(getActivity(), tushuoid);
    }

    private class TushuoAdapter extends BaseAdapter {

        private int mItemHeight = 0;

        private ListView.LayoutParams mItemLayoutParams;

        public TushuoAdapter() {
            mItemLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
        }

        @Override
        public int getCount() {
            return mData != null ? mData.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return mData != null ? mData.get(position) : 0;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            CBHItemTushuo cbhItemView = null;
            if (convertView == null) {
                convertView = (CBHItemTushuo) View
                        .inflate(getActivity(), R.layout.item_tushu, null);
                convertView.setLayoutParams(mItemLayoutParams);
            }
            cbhItemView = (CBHItemTushuo) convertView;

            if (cbhItemView.getLayoutParams().height != mItemHeight) {
                cbhItemView.setLayoutParams(mItemLayoutParams);
            }

            TushuoItem item = mData.get(position);
            if (item != null) {
                cbhItemView.setName(item.getName());
                cbhItemView.setCount(item.getTushuoCount());
                PicModel picModel = item.getPicModel();
                if (picModel != null) {
                    String url = item.getPicModel().url;
                    cbhItemView.loadImage(mImageFetcher, url);
                } else {
                    cbhItemView.setEmptyImage(R.drawable.empty_photo);
                }
            }
            //
            return cbhItemView;
        }

        public void setItemHeight(int height) {
            if (height == mItemHeight) {
                return;
            }
            mItemHeight = height;
            mItemLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
            // mImageFetcher.setImageSize(height);
            notifyDataSetChanged();
        }
    }

    public static CBHTuShuoFragment newInstance(FragmentManager fm) {
        final CBHTuShuoFragment f = new CBHTuShuoFragment(fm);
        return f;
    }

}
